package com.jx.driving.protocol.terminal.codec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;

/**
 * byte类型读取写入器
 * 
 * @author shenliuyang
 *
 *         2016年7月29日下午6:24:22
 */
public class ByteFieldReaderWriter extends FieldReaderWriter {

	public ByteFieldReaderWriter(Field field, int size, int order) {
		super(field, 1, order);
	}

	@Override
	public void read(InputStream in, Object to) throws IOException {
		byte data = (byte) in.read();
		short value = (short) (data & 0xFF);
		wrtietoField(to, value);
	}

	@Override
	public void write(OutputStream out, Object from) throws IOException {
		Number data = (Number) readFromField(from);
		if (data == null) {
			data = 0;
		}
		out.write(data.byteValue());
	}
}